import java.util.HashSet;
import java.util.Set;

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        Set<Character> set = new HashSet<>();
        int n = s.length();
        int maxLen = 0;
        int right = 0;
        for(int i = 0; i < n; i++){

            if (i != 0) {
                set.remove(s.charAt(i - 1));
            }
            while(right < n && !set.contains(s.charAt(right))){
                set.add(s.charAt(right));
                right++;
            }
            maxLen = Math.max(maxLen, right - i);
        }
        return maxLen;

    }
}
